package dao;

import domin.Actor;
import domin.OrdinaryUser;
import com.alibaba.fastjson.JSON;
import domin.Users;
import lombok.extern.slf4j.Slf4j;
//import rsgl.service.DepartmentService;
//import rsgl.service.PostService;
import util.CommonDao;
import util.Condition;
import util.JdbcHelper;
import util.Pagination;

import java.sql.*;
import java.util.Collection;
import java.util.HashSet;
import java.util.TreeSet;
@Slf4j public final class ActorDao {
    private static ActorDao actorDao = new ActorDao();
    PreparedStatement preparedStatement = null;
    ResultSet resultSet = null;
    private ActorDao() {
    }
    public static ActorDao getInstance() {
        return actorDao;
    }

    public Collection<Actor> findAll(Connection connection, Collection<Condition> condition, Pagination pagination) throws SQLException {
        Collection<Actor> actors = new TreeSet<Actor>();
        int totalNum = CommonDao.getInstance().count(connection,"Actor");
        String sql_select_all = "SELECT * FROM actor ";
        if (condition != null){
            sql_select_all += Condition.toWhereClause(JSON.toJSONString(condition));
            log.trace(sql_select_all);
        }
        if (pagination != null){
            sql_select_all += pagination.toLimitClause(totalNum);
            log.trace(sql_select_all);
        }
        this.preparedStatement = connection.prepareStatement(sql_select_all);
        this.resultSet = preparedStatement.executeQuery();
        while (this.resultSet.next()){
            int id = resultSet.getInt("id");
            String name = resultSet.getString("name");
            Actor foundActor = new Actor(name,id);
            actors.add(foundActor);
        }
        JdbcHelper.close(this.resultSet,this.preparedStatement);
        return actors;
    }
    public Actor find(Connection connection, Integer id) throws SQLException {
        Actor foundActor = new Actor();
        this.preparedStatement = connection.prepareStatement("select * from actor where id=" +id);
        this.resultSet = this.preparedStatement.executeQuery();
        this.resultSet.next();
        foundActor.setId(this.resultSet.getInt("id"));
        foundActor.setName(this.resultSet.getString("name"));
        JdbcHelper.close(resultSet,preparedStatement);
        return foundActor;
    }
    public Collection<Actor> getAllWithCondition(Connection connection, String s) throws SQLException{
        Collection<Actor> actors = new HashSet<Actor>();
        String value = "%" + s + "%";
        value=" \"" + value + "\"";
        PreparedStatement preparedStatement = connection.prepareStatement("" +
                "select * from actor where " +
                "name like " + value);
        ResultSet resultSet = preparedStatement.executeQuery();
        while (resultSet.next()) {
            Actor actor = null;
            Integer id = resultSet.getInt("id");
            String name = resultSet.getString("name");
            actor = new Actor(name,id);
            actors.add(actor);
        }
        JdbcHelper.close(null,preparedStatement);
        return actors;
    }
    public boolean update(Connection connection, Actor actor) throws SQLException {
        this.preparedStatement = connection.prepareStatement("update actor set name=? where id=?");
        this.preparedStatement.setString(1, actor.getName());
        int rowAffected = this.preparedStatement.executeUpdate();
        JdbcHelper.close(null,preparedStatement);
        return rowAffected > 0;
    }
    public int add(Connection connection,Actor actor) throws SQLException {
        this.preparedStatement = connection.prepareStatement("INSERT INTO actor (name) VALUES (?)",Statement.RETURN_GENERATED_KEYS);
        preparedStatement.setString(1, actor.getName());
        int idOfAddedActor = 0;
        ResultSet resultSet = preparedStatement.getGeneratedKeys();
        if (resultSet.next()) {
            idOfAddedActor = resultSet.getInt(1);
        }
        JdbcHelper.close(resultSet,preparedStatement);
        return idOfAddedActor;
    }

    public boolean delete(Connection connection,Integer id) throws SQLException {
        this.preparedStatement = connection.prepareStatement("delete from actor where id=" + id);
        int rowAffected = preparedStatement.executeUpdate();
        JdbcHelper.close(null,preparedStatement);
        return rowAffected>0;
    }
}
